﻿using System;
using System.Diagnostics;
using System.ServiceModel;
using System.ServiceProcess;
using AccessControl.Utility.Tools;
using Timer = System.Timers.Timer;

namespace AccessControl.AccessControlAgent
{
    public partial class AccessControlAgent : ServiceBase
    {
        private Timer timerKeepAlive;
        public static AccessControlAgent ServiceInstance { get; private set; }
        private ServiceHost acServiceAgent;

        public AccessControlAgent()
        {
            InitializeComponent();


        }

        protected override void OnStart(string[] args){
            ServiceInstance = this;

            WriteLog("Agent Started for service "+Properties.Settings.Default.ServiceName + " interval:"+Properties.Settings.Default.Interval, eventLevelConstants.Info);

            try{
                acServiceAgent = new ServiceHost(typeof(ACServiceAgent));
                acServiceAgent.Open();
                WriteLog("Agent Service started ", eventLevelConstants.Info);
            }
            catch (Exception ex){
                WriteLog("Agent Service couldn't be started, "+ex.Message, eventLevelConstants.Error);
            }

            try{
                timerKeepAlive=new Timer(Properties.Settings.Default.Interval);
                timerKeepAlive.Elapsed += Timer_Elapsed;
                if (timerKeepAlive.Interval == 0) timerKeepAlive.Interval = 15000;
                timerKeepAlive.Enabled = true;

                if (!ServiceManager.FindService()){
                    WriteLog("Service " + Properties.Settings.Default.ServiceName + " Not Found!", eventLevelConstants.Error);
                    Stop();
                }

                timerKeepAlive.Start();

            }
            catch (Exception ex){
                WriteLog("Error starting service, "+ex.Message, eventLevelConstants.Error);
                Stop();
            }

        }

        protected override void OnStop()
        {
            acServiceAgent.Close();
            WriteLog("Agent Stopped", eventLevelConstants.Info);
        }

        private void Timer_Elapsed(object sender, EventArgs e)
        {
            try{
                timerKeepAlive.Stop();
                ServiceManager.CheckAlive();
            }
            catch (Exception ex){
                WriteLog("Error in timer, "+ex.Message,eventLevelConstants.Error);
            }finally{
                timerKeepAlive.Start();
            }
        }

        public void WriteLog(String message, eventLevelConstants level){
            EventLogEntryType logType;
            switch(level){
                case eventLevelConstants.Error:
                    logType = EventLogEntryType.Error;
                    break;
                case eventLevelConstants.Warning:
                    logType = EventLogEntryType.Warning;
                    break;
                default:
                    logType = EventLogEntryType.Information;
                    break;
            }

            EventLog.WriteEntry("ACAgent",message,logType);

            try{
                LogFile.WriteLog(LogFile.DefaultLogfileLocation + "AccessControlAgent\\" + DateTime.Now.Date.ToString("yyyyMMdd") + ".txt", level, message);
            }
            catch{
                EventLog.WriteEntry("ACAgent","Can not write to log file!",  EventLogEntryType.Warning);
            }
        }

        public void StopMonitoring(){
            timerKeepAlive.Stop();
        }
        public void StartMonitoring(){
            timerKeepAlive.Start();
        }
    }
}
